<?php
include_once('lib/common.php');


class vmw_Host extends vmw_Page
{
    protected $props;


    public function __construct($title = 'Host Info')
    {
        parent::__construct($title);
    }


    protected function GetJQBody()
    {
        //
        // Get the status of the virtual machine in a human readable
        // form.
        //
        if ($this->props['summary.runtime.powerState'] == 'poweredOn')
            $state = 'On';
        else if ($this->props['summary.runtime.powerState'] == 'poweredOff')
            $state = 'Off';
        else if ($this->props['summary.runtime.powerState'] == 'standBy')
            $state = 'Standby';
        else
            $state = 'Unknown';
        if ($this->props['summary.runtime.inMaintenanceMode'] == true)
            $state .= ' <span class="errorText">(Maintenance Mode)</span>';
        if ($this->props['summary.runtime.connectionState'] != 'connected')
            $state .= ' <span class="errorText">(Not Connected)</span>';

        $content =  '<div data-role="collapsible" data-collapsed="false" data-content-theme="d">' . "\r\n";
        $content .= '  <h3 style="margin: 0px;">Summary</h3>' . "\r\n";
        $content .= '  <ul data-role="listview" data-inset="true">' . "\r\n";
        $content .= $this->GetTitleValuePair('Name', $this->props['summary.config.name']);
        $content .= $this->GetTitleValuePair('State', $state, true);
        $content .= $this->GetTitleValuePair('Version', $this->props['summary.config.product.fullName']);
        $content .= $this->GetTitleValuePair('VMotion', ($this->props['summary.config.vmotionEnabled'] == true ? 'Enabled' : 'Disabled'));
        $content .= $this->GetTitleValuePair('Fault Tolerance', ($this->props['summary.config.faultToleranceEnabled'] == true ? 'Enabled' : 'Disabled'));
        $content .= '  </ul>';
        $content .= '</div>';

        $content .= '<div data-role="collapsible" data-collapsed="false" data-content-theme="d">' . "\r\n";
        $content .= '  <h3 style="margin: 0px;">Hardware</h3>' . "\r\n";
        $content .= '  <ul data-role="listview" data-inset="true">' . "\r\n";
        $content .= $this->GetTitleValuePair('Vendor', $this->props['summary.hardware']['vendor']);
        $content .= $this->GetTitleValuePair('Model', $this->props['summary.hardware']['model']);
        $content .= $this->GetTitleValuePair('CPU Type', $this->props['summary.hardware']['cpuModel']);
        $content .= $this->GetTitleValuePair('CPUs', $this->props['summary.hardware']['numCpuPkgs'] . ' @ ' . $this->props['summary.hardware']['cpuMhz'] . 'MHz');
        $content .= $this->GetTitleValuePair('Cores', $this->props['summary.hardware']['numCpuCores']);
        $content .= $this->GetTitleValuePair('Threads', $this->props['summary.hardware']['numCpuThreads']);
        $content .= $this->GetTitleValuePair('RAM', round($this->props['summary.hardware']['memorySize'] / 1024 / 1024 / 1024, 2) . 'GB');
        $content .= $this->GetTitleValuePair('NICs', $this->props['summary.hardware']['numNics']);
        $content .= '  </ul>';
        $content .= '</div>';

        $content .= '<div data-role="collapsible" data-collapsed="false" data-content-theme="d">' . "\r\n";
        $content .= '  <h3 style="margin: 0px;">Status</h3>' . "\r\n";
        $content .= '  <ul data-role="listview" data-inset="true">' . "\r\n";
        $content .= $this->GetTitleValuePair('Overall Status', vic_Enum::ManagedEntityStatus($this->props['summary.overallStatus'], vic_Enum::AS_HTML), true);
        $content .= $this->GetTitleValuePair('CPU Usage', $this->props['summary.quickStats']['overallCpuUsage'] . 'MHz');
        $content .= $this->GetTitleValuePair('Memory Usage', $this->props['summary.quickStats']['overallMemoryUsage'] . 'MB');
        $content .= $this->GetTitleValuePair('Uptime', $this->SecondsToTime($this->props['summary.quickStats']['uptime']));
        foreach ($this->props['summary.runtime.healthSystemRuntime.hardwareStatusInfo'] as $k => $v)
        {
            if (isset($v['status']))
            {
                if ($v['status']['label'] == 'Green')
                    $content .= $this->GetTitleValuePair($v['name'], $v['status']['label']);
                else
                    $content .= $this->GetTitleValuePair($v['name'], $v['status']['label'] . ' - ' . $v['status']['summary']);
            }
            else
            {
                foreach($v as $k2 => $v2)
                {
                    if ($v2['status']['label'] == 'Green')
                        $content .= $this->GetTitleValuePair($v2['name'], $v2['status']['label']);
                    else
                        $content .= $this->GetTitleValuePair($v2['name'], $v2['status']['label'] . ' - ' . $v2['status']['summary']);
                }
            }
        }
        $content .= '  </ul>';
        $content .= '</div>';

        return $content;
    }


    private function GetTitleValuePair($title, $value, $rawvalue = false)
    {
        if ($rawvalue == false)
            $value = htmlspecialchars($value);
        if ($value == '')
            $value = '&nbsp;';

        $content =  '    <li style="padding-top: 3px; padding-bottom: 3px;" data-theme="c">' . "\r\n";
        $content .= '      <p style="margin-top: 0px;"><strong>' . htmlspecialchars($title) . '</strong></p>' . "\r\n";
        $content .= '      <p style="margin-bottom: 0px;">' . $value . '</p>' . "\r\n";
        $content .= '    </li>' . "\r\n";

        return $content;
    }


    private function SecondsToTime($seconds)
    {
        $time = '';

        if ($seconds == '')
            return '';

        if ($seconds >= 86400)
        {
            $num = ($seconds / 86400);
            $seconds %= 86400;

            if ($num > 0)
                $time .= sprintf('%d days, ', $num);
        }

        if ($seconds >= 3600)
        {
            $num = ($seconds / 3600);
            $seconds %= 3600;

            if ($num > 0)
                $time .= sprintf('%d:', $num);
        }

        $min = ($seconds / 60);
        $seconds %= 60;
        $time .= sprintf('%02d:%02d', $min, $seconds);

        return trim($time);
    }


    public function ProcessRequest()
    {
        global $vmware;

        $p = array( 'summary.hardware',
		'summary.runtime.connectionState',
		'summary.runtime.powerState',
		'summary.runtime.inMaintenanceMode',
		'summary.runtime.healthSystemRuntime.hardwareStatusInfo',
		'summary.config.name',
		'summary.config.product.fullName',
		'summary.config.vmotionEnabled',
		'summary.config.faultToleranceEnabled',
		'summary.quickStats',
		'summary.overallStatus'
		);

        //
        // Get the information about the virtual machine and the host name.
        //
        $host = vic_ManagedObjectReference::HostSystem($_REQUEST['host']);
        $this->props = $vmware->GetObjectProperties($host, $p);

        $this->SetPageTitle($this->props['summary.config.name']);
        $this->pageHeader = vic_Enum::ManagedEntityStatus($this->props['summary.overallStatus'], vic_Enum::AS_IMAGE, array('class' => 'headerIcon')) .
            ' ' . htmlspecialchars($this->props['summary.config.name']);

        parent::ProcessRequest();
    }
}

vmware_session();
$page = new vmw_Host();
$page->ProcessRequest();
?>
